package simple;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

public class simpleServer {

	private static ServerSocketChannel listenerServer, readwriteServer;
	private static Selector listenerSelector;
	private static Selector readSelector;
	private static Selector writeSelector;
	
	private static SocketChannel readChannel;
	private static SocketChannel writeChannel;
	
	private static ByteBuffer buf = ByteBuffer.allocate(1024);
	
	public static void main(String[] args) {
		try {
			listenerServer = ServerSocketChannel.open();
			listenerServer.configureBlocking(false);
			listenerServer.socket().bind(new InetSocketAddress("localhost", 1234));
			
			readwriteServer = ServerSocketChannel.open();
			readwriteServer.configureBlocking(false);
			readwriteServer.socket().bind(new InetSocketAddress("localhost", 4444));
			
			listenerSelector = Selector.open();
			readSelector = Selector.open();
			writeSelector = Selector.open();
			
			listenerServer.register(listenerSelector, SelectionKey.OP_ACCEPT);
			
			
			while (true){
				listenerSelector.select();
			
				Set keys = listenerSelector.selectedKeys();
				Iterator iter = keys.iterator();	
				
				  
				while (iter.hasNext()){
					
					  System.out.println("Iteration");
					  // Get this most recent key
					  SelectionKey key = (SelectionKey) iter.next();
					  
					  //if (iter.hasNext())
						 //iter.remove();					  
					  
					  if (key.isAcceptable()){
						  System.out.println("Acceptable");
						  
						  SocketChannel acceptChannel = listenerServer.accept();
						  
						  acceptChannel.configureBlocking(false);
						  
						  acceptChannel.register(listenerSelector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
						  iter.remove();
					  }
					  
					  if (key.isConnectable()){
						  System.out.println("Connectable");					  
					  }
					  
					  if (key.isWritable()){
						  System.out.println("Writable");
						  						  
						  
						  writeChannel = (SocketChannel) key.channel();
						  writeChannel.configureBlocking(false);
						  
						  writeChannel.register(listenerSelector, SelectionKey.OP_READ);
						  
						  while(writeChannel.isConnectionPending()){
							  writeChannel.finishConnect();
						  }
						  
						  //int n = readBuffer(writeChannel);
						  int n = writeChannel.write(ByteBuffer.wrap(new String("Back at you!").getBytes()));
						  System.out.println(n);
						  						  
						  iter.remove();
						  
					  }
					  
					  if (key.isReadable()){
						  System.out.println("Readable");
						  						  
					  }
					  
					  
				}
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		

	}
	
	private static int readBuffer(SocketChannel channel){
		ByteBuffer buf = ByteBuffer.allocateDirect(1024);
		  buf.clear();
		  int n = -99;
		  try{
			  for(;;){
				  n = channel.read(buf);
				  System.out.println(n);
		  	  
				  if (n == -1)
					  break;
				  else{
					  buf.flip();
					  byte[] temp = new byte[buf.remaining()];
					  buf.get(temp);
				  
					  String s = new String(temp);
					  System.out.println(s);
				  }			  
			  }
		  }catch (Exception e){
			  e.printStackTrace();
		  }
		  
		  return n;
	}

}
